package mms.farina.thecell.activities;


import mms.farina.thecell.R;
import mms.farina.thecell.services.GeoInfoService;
import mms.farina.thecell.services.ReceiveService;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**Activity per la visualizzazione delle mappe nella versione 2*/
public class Map extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		updateOverlay();

	    
	}
	
	
	private void updateOverlay(){

		GoogleMap mMap;
		mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		
		if(ReceiveService.geoInfo==null)return;
		for(String k: ReceiveService.geoInfo.keySet()){
			double[] coordinate=ReceiveService.geoInfo.get(k);
			if(coordinate[0]==GeoInfoService.NO_COORDINATE)continue;
			int resourceIcon=0;
			if(k.equals(ReceiveService.NAME))resourceIcon=R.drawable.redflag;
			else resourceIcon=R.drawable.greenflag;
			
			 mMap.addMarker(new MarkerOptions()
	         .position(new LatLng(coordinate[0], coordinate[1]))
	         .title(k)
	         .snippet("Posizione Corrente")
	         .icon(BitmapDescriptorFactory.fromResource(resourceIcon)));	
		    
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_map, menu);
		return true;
	}

	public void refreshButtonListener(View v){
		updateOverlay();
	}
	
	public void chageViewListener(View v){

		RadioGroup rbg=(RadioGroup) findViewById(R.id.mapGroup);
		RadioButton rb=(RadioButton) findViewById(rbg.getCheckedRadioButtonId());
		
		GoogleMap mMap;
		mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		
		if(rb.getText().equals("Simple"))mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		else if(rb.getText().equals("Hybrid"))mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		else mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		
	}
	
	@Override
	protected void onStop(){
		super.onStop();
    	finish();
	}

	@Override
	protected void onPause(){
		super.onPause();
    	finish();
	}

}
